package com.study.manage.dal;

import java.util.Date;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;

import com.study.manage.domain.Blog;
import com.study.manage.domain.enumm.BlogType;
import com.study.manage.domain.enumm.EvaluateApplyVerifyState;


public interface BlogRepository extends PagingAndSortingRepository<Blog, Long> {

    List<Blog> findByTypeAndExtId(BlogType type, Long extId);

    Page<Blog> findByTypeAndUserId(BlogType type, Long userId, Pageable pageable);

    @Query("select count(t) from Blog t,EvaluateApply ea where t.type=6 and ea.id=t.extId and ea.evaluateId in (:evaluateId) and (to_days(t.createdAt)=to_days(:date) or :date is null)")
    int countByTypeAndEvaluateIdAndDate(@Param("evaluateId") List<Long> evaluateIds,
            @Param("date") Date date);

    @Query("select t from Blog t,EvaluateApply ea where t.type=6 and ea.id=t.extId and t.state=0 and ea.state=0"
            + " and ea.evaluateId=:evaluateId and (ea.verifyState=:verifyState or :verifyState is null)"
            + " and (ea.fullName=:name or ea.number=:name or ea.phone=:name or ea.groupName=:name or :name is null) "
            + " and (ea.userId=:userId or ea.referenceId=:userId or :userId is null) "
            + " and (to_days(ea.passAt)=to_days(:passAt) or :passAt is null)")
    Page<Blog> findWorkByEvaluateId(@Param("evaluateId") Long evaluateId,
            @Param("verifyState") EvaluateApplyVerifyState verifyState, @Param("name") String name,
            @Param("userId") Long userId, @Param("passAt") Date passAt, Pageable pageable);

}
